package com.symc.sims0213xfw.controller.student;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.symc.sims0213xfw.bean.Classes;
import com.symc.sims0213xfw.bean.Department;
import com.symc.sims0213xfw.service.impl.ClassesServiceImpl;
import com.symc.sims0213xfw.service.impl.CollegeServiceImpl;
import com.symc.sims0213xfw.service.impl.DepartmentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: 凤文  沈阳医学院2019级医学信息工程 0213
 * @CreateTime: 2022/01/27 22:00
 * @Description:
 */
@WebServlet("/multistageTandemServlet")
public class MultistageTandemServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String college = request.getParameter("selectedCollege");
        String department = request.getParameter("selectedDepartment");
        String grade = request.getParameter("selectedGrade");

        if (college != null) {
            List<Department> optionalDepartmentList = new DepartmentServiceImpl().getOptionalDepartmentList(college);
            ObjectMapper mapper = new ObjectMapper();
            String departmentJson = mapper.writeValueAsString(optionalDepartmentList);
            out.write(departmentJson);
        }

        if (department != null) {
            List<Classes> optionalGrade = new ClassesServiceImpl().getOptionalGrade(department);
            ObjectMapper mapper = new ObjectMapper();
            String gradeJson = mapper.writeValueAsString(optionalGrade);
            out.write(gradeJson);
        }

        if (grade != null) {
            List<Classes> optionalClassesName = new ClassesServiceImpl().getOptionalClassesName(Integer.parseInt(grade));
            String classesNameJson = new ObjectMapper().writeValueAsString(optionalClassesName);
            out.write(classesNameJson);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
